//
//  HomeRefresh.swift
//  weibo_swift
//
//  Created by dulinlong on 15/7/6.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit
import FFAutoLayout


class HomeRefreshControl: UIRefreshControl {
    
    override init(){
        super.init()
        addSubview(refreshView)
        
        refreshView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: CGSize(width: 160, height: 60))
        
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        
    }
    
    deinit{
        removeObserver(self, forKeyPath: "frame")
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //上下拉标志
    var pullRefreshTag = false
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        if frame.origin.y > 0 {
            return
        }
        
        if refreshing {
            //开始刷新动画
            refreshView.startLeadingAnim()
        }
        
        
        //反转加载箭头
        if frame.origin.y < -50 && !pullRefreshTag{

            refreshView.rotatePullRefresh(pullRefreshTag)
            pullRefreshTag = !pullRefreshTag
            
        }else if frame.origin.y > -50 && pullRefreshTag{
            
            refreshView.rotatePullRefresh(pullRefreshTag)
            pullRefreshTag = !pullRefreshTag
        }
    }
    
    override func endRefreshing() {
       super.endRefreshing()
        //结束刷新动画
        refreshView.endLeadingAnim()
        
        
    }
    
    lazy var refreshView: HomeRefreshView = HomeRefreshView.refreshView()

}


class HomeRefreshView: UIView {
    
    @IBOutlet weak var pullRefreshImageView: UIImageView!
    
    @IBOutlet weak var leadingImageView: UIImageView!
    @IBOutlet weak var leadingView: UIView!
    
    class func refreshView() -> HomeRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("HomeRefresh", owner: nil, options: nil).last as! HomeRefreshView
    }
    
    //旋转
    func rotatePullRefresh(colockWise: Bool){
        
        var angle = CGFloat(M_PI)
        angle += colockWise ? 0.01 : -0.01
        UIView.animateWithDuration(0.5) { () -> Void in

            self.pullRefreshImageView.transform = CGAffineTransformRotate(self.pullRefreshImageView.transform, angle)
        }
    }
    
    private func startLeadingAnim(){

        leadingView.hidden = false
        //判断是不是已经有动画了
        if self.leadingImageView.layer.animationForKey("leadingImageView") != nil{
            return
        }
        
       let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 5
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        anim.fillMode = kCAFillModeForwards
        
        leadingImageView.layer.addAnimation(anim, forKey: "leadingImageView")
        
    }
    
    private func endLeadingAnim(){
        
        leadingView.hidden = true
        leadingImageView.layer.removeAllAnimations()
    }
}